var Amqp = require('../amqp').Amqp;
/**
 * Work with standard transactions.
 *
 * Standard transactions provide so-called "1.5 phase commit". We can ensure that work is never
 * lost, but there is a chance of confirmations being lost, so that messages may be resent.
 * Applications that use standard transactions must be able to detect and ignore duplicate
 * messages.
 *
 * @class
 * @extends events.EventEmitter
 * @exports Tx as impl.Tx
 * @param handler
 */
function Tx(/**handlers.Channel*/handler){
   var tx = this;
   this.handler = handler;
   /**
    * Confirm transaction mode.
    *
    * This method confirms to the client that the channel was successfully set to use standard
    * transactions.
    *
    * @name Tx#select
    * @event
    */
   handler.addListener('Amqp.Tx.SelectOk', function(){
       tx.emit('select');
   });
   /**
    * Confirm a successful commit.
    *
    * This method confirms to the client that the commit succeeded. Note that if a commit fails,
    * the server raises a channel exception.
    *
    * @name Tx#commit
    * @event
    */
   handler.addListener('Amqp.Tx.CommitOk', function(){
       tx.emit('commit');
   });
   /**
    * Confirm a successful rollback.
    *
    * This method confirms to the client that the rollback succeeded. Note that if an rollback
    * fails, the server raises a channel exception.
    *
    * @name Tx#rollback
    * @event
    */
   handler.addListener('Amqp.Tx.RollbackOk', function(){
       tx.emit('rollback');
   });
}
require('sys').inherits(Tx, require('events').EventEmitter);
/** Commit the current transaction.
 *
 * This method commits all messages published and acknowledged in the current transaction.
 * A new transaction starts immediately after a commit. */
Tx.prototype.commit = function(){
    this.handler.call(Amqp.Class.Tx, Amqp.Class.Tx.Commit);
};
/** Abandon the current transaction.
 *
 * This method abandons all messages published and acknowledged in the current transaction.
 * A new transaction starts immediately after a rollback. */
Tx.prototype.rollback = function(){
    this.handler.call(Amqp.Class.Tx, Amqp.Class.Tx.Rollback);
};
/** Select standard transaction mode.
 *
 * This method sets the channel to use standard transactions. The client must use this method at
 * least once on a channel before using the Commit or Rollback methods. */
Tx.prototype.select = function(){
    this.handler.call(Amqp.Class.Tx, Amqp.Class.Tx.Select);
};
exports.Tx = Tx;
